package logic

import (
	"context"
	"goods/model/mysql"

	"goods/goodsrpc/goodsrpc"
	"goods/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsShowLogic {
	return &GoodsShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsShowLogic) GoodsShow(in *goodsrpc.GoodsShowRequest) (*goodsrpc.GoodsShowResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.GoodsData

	mysql.DB.Debug().Model(&mysql.Goods{}).
		Select("goods.*, classes.name as class_name,brands.name as brand_name").
		Joins("left join classes on classes.id = goods.class_id").
		Joins("left join brands on brands.id = goods.brand_id").
		Where("goods.id = ?", in.GoodsId).Find(&goods)

	return &goodsrpc.GoodsShowResponse{
		Name:      goods.Name,
		Image:     goods.Image,
		Price:     float32(goods.Price),
		ClassName: goods.ClassName,
		BrandName: goods.BrandName,
		GoodsId:   int64(goods.ID),
	}, nil
}
